/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.message;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.message.Message;
import com.unis.service.message.MessageService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-02-24
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    private MessageService messageService;

    /****************************************
     * Insert a instace of Message
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertMessage(@RequestBody Message message) throws Exception {
        message.setStatmentId("Message.insert");
        String status = "failure";
        messageService.insert(message);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Message
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Message message) throws Exception {
        message.setStatmentId("Message.update");
        String status = "failure";
        messageService.update(message);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Message
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Message message) throws Exception {
        message.setStatmentId("Message.update");
        String status = "failure";
        messageService.batchUpdate(message);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Message
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Message message = new Message();
        if(paramMap.containsKey("all")){
            message.setXxzt(null);
        }
        message.setStatmentId("Message.queryAsList");
        BeanUtils.populate(message, paramMap);
        if (!message.isPaging()) {
            return messageService.queryAsList(message);
        }
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(message, pageMap);
        List<BaseModel> messageList = messageService.queryAsList(message);
        Map<String, Object> renderMap = GridUIMapper.render(message, messageList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Message
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Message message) throws Exception {
        message.setStatmentId("Message.queryAsObject");
        return messageService.queryAsObject(message);
    }

    @RequestMapping("/queryNewMsg")
    public String queryNewMsg(ModelMap map) throws Exception {
        Subject currentUser = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) currentUser.getPrincipal();

        Message message = new Message();
        message.setStatmentId("Message.queryRecordsCount");
        message.setXxjsrid(sysUser.getUserId());
        message.setXxjsrxm(sysUser.getUsername());
        message = (Message) messageService.queryAsObject(message);

        map.addAttribute("msgCount", message.getTotalCount());
        return "message/messageTemplate";
    }

    @RequestMapping("/page")
    public String forword() {
        return "message/messageList";
    }

    @Resource
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
}
